import argparse
import random
import numpy as np
from omegaconf import OmegaConf
import torch
import importlib

def parser_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('--config', type=str, default='./config/PFNN.yaml')
    return parser.parse_args()

def parser_from_config(config_path):
    config = OmegaConf.load(config_path)
    return config

def print_config(config):
    for k, v in config.items():
        print(f"{k}: {v}")

def seed_everything(seed):
    random.seed(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)

def get_obj_from_str(string, reload=False):
    module, cls = string.rsplit(".", 1)
    if reload:
        module_imp = importlib.import_module(module)
        importlib.reload(module_imp)
    return getattr(importlib.import_module(module, package=None), cls)

def instaniate_from_config(config):
    if not "target" in config:
        raise KeyError("Expected key `target` to instaniate.")
    return get_obj_from_str(config["target"])(**config.get("params", dict()))